package com.exmaple.lemlin.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.exmaple.lemlin.R;
import com.exmaple.lemlin.app.APPIM;
import com.exmaple.lemlin.app.Constants;
import com.exmaple.lemlin.entity.ChatMessage;
import com.exmaple.lemlin.entity.ChatMessageDao;
import com.exmaple.lemlin.entity.ChatRecord;
import com.exmaple.lemlin.entity.ChatRecordDao;
import com.exmaple.lemlin.ui.abstracts.AbstractBaseActivity;
import com.exmaple.lemlin.ui.adapter.ChatWindowsAdapter;
import com.exmaple.lemlin.utils.LogUtils;
import com.exmaple.lemlin.xmpp.XmppConnection;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ChatWindowsActivity extends AbstractBaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.edit_content)
    EditText editContent;

    private String userName;

    private ChatWindowsAdapter windowsAdapter;
    private LinearLayoutManager layoutManager;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_chat_windows;
    }

    @Override
    protected void initView() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        userName = getIntent().getStringExtra(Constants.BUNDLE_KEY_USER_NAME);
        getSupportActionBar().setTitle(userName);


        List<ChatMessage> messages = APPIM.getDaoSession().getChatMessageDao()
                .queryBuilder().where(ChatMessageDao.Properties.Name.eq(userName)).list();
        windowsAdapter = new ChatWindowsAdapter((this), messages);
        layoutManager = new LinearLayoutManager((this));
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(windowsAdapter);
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    public void onEventMainThread(ChatMessage message) {
        super.onEventMainThread(message);
        layoutManager.scrollToPositionWithOffset(windowsAdapter.getItemCount(), (0));
        windowsAdapter.addItem(message);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        ChatRecordDao recordDao = APPIM.getDaoSession().getChatRecordDao();
        ChatRecord record = recordDao.queryBuilder()
                .where(ChatRecordDao.Properties.Name.eq(userName)).unique();
        if (record != null) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra(Constants.BUNDLE_KEY_USER_NAME, userName);
            intent.putExtra(Constants.BUNDLE_KEY_COUNT_UNREAD, record.getLastMsgCount());
            setResult(RESULT_OK, intent);
        }
        finish();
    }

    @OnClick({R.id.btn_send})
    public void onViewClicked(View view) {
        String content = editContent.getText().toString();
        if (TextUtils.isEmpty(content)) {
            Toast.makeText((this), ("内容不能为空"), Toast.LENGTH_SHORT).show();
            return;
        }
        if (!TextUtils.isEmpty(content)) {
            XmppConnection.getInstance().sendChatMessage(userName, content);
            editContent.setText(null);
        }
    }

}
